<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Http\Service\api\MessageService;
use App\Models\UserNotice;
use App\Models\UserNoticeSee;
use Illuminate\Http\Request;

class MessageController extends Controller
{
    //
    //商户端站内信


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 站内信查询
     */
    public function messageSelect(Request $request)
    {
        $uid = $request->input('uid');
        $true = MessageService::UserMessageSelect($uid);
        if ($true['code']==200){
            return json_encode(['data'=>$true['data'],'msg'=>$true['msg'],'code'=>200]);
        }
        return json_encode(['data'=>'','msg'=>$true['msg'],'code'=>400]);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     * 用户站内信添加（用户通知）
     */
    public function messageAdd(Request $request)
    {
        $data['uid'] = $request->input('uid');
        $data['type'] = $request->input('type');
        $data['user'] = $request->input('user');
        $data['title'] = $request->input('title');
        $data['content'] = $request->input('content');
        $data['send_time'] = $request->input('send_time');
        if (empty($data)){
            return json_encode(['data'=>'','msg'=>'数据异常','code'=>400]);
        }
        $true = MessageService::UserMessageAdd($data);
        return $true;
        if ($true['code']==200){
            return json_encode(['data'=>'','msg'=>$true['msg'],'code'=>200]);
        }
        return json_encode(['data'=>'','msg'=>$true['msg'],'code'=>400]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 用户站内信删除
     */
    public function userMessageDel(Request $request)
    {
        $id = $request->input('id');
        $true = MessageService::UserMessageDel($id);
        if ($true['code']==200){
            return json_encode(['data'=>'','msg'=>$true['msg'],'code'=>200]);
        }
        return json_encode(['data'=>'','msg'=>$true['msg'],'code'=>400]);

    }

    /**
     * 商户端公告(后台发送系统消息)
     */
    public function notice(Request $request){
        $data = $request->all();
        $data['type'] = 1;
        $data['uid'] = 0;
        UserNotice::create($data);
    }
}
